/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugInventoryCheck;
import com.jims.master.modules.drug.entity.DrugStock;
import com.jims.master.modules.drug.service.DrugInventoryCheckService;
import com.jims.master.modules.drug.service.DrugStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 药品盘点Controller
 * @author zhuq
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugInventoryCheck")
public class DrugInventoryCheckController extends BaseController {

	@Autowired
    private DrugInventoryCheckService drugInventoryCheckService;
    @Autowired
    private DrugStockService drugStockService;

	@ModelAttribute
	public DrugInventoryCheck get(@RequestParam(required=false) String id) {
		DrugInventoryCheck entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugInventoryCheckService.get(id);
		}
		if (entity == null){
			entity = new DrugInventoryCheck();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugInventoryCheck drugInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugStock> page = drugStockService.findInventoryStockPage(new Page<DrugStock>(request, response), drugInventoryCheck);
        model.addAttribute("page", page);
		return "modules/drug/drugInventoryCheckIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugInventoryCheck drugInventoryCheck, Model model) {
		model.addAttribute("drugInventoryCheck", drugInventoryCheck);
		return "modules/drug/drugInventoryCheckForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugInventoryCheck drugInventoryCheck, Model model) {
	    StringData data=new StringData();
		int i=drugInventoryCheckService.save(drugInventoryCheck);
        if(i>0){
            data.setCode("success");
            data.setData("保存药品盘点成功");
        }else{
            data.setCode("error");
            data.setData("保存药品盘点失败");
        }
        return data;
	}

    /*
    * 药品盘点，将一个药品不同批次的盘点数据保存
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveList")
    @ResponseBody
    public StringData saveList(@RequestBody List<DrugInventoryCheck> DrugInventoryCheckList,HttpServletRequest request, HttpServletResponse response) {
        StringData data=new StringData();
        int i=drugInventoryCheckService.saveList(DrugInventoryCheckList);
        if(i>0){
            data.setCode("success");
        }else{
            data.setCode("error");
        }
        return data;
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugInventoryCheck drugInventoryCheck) {
	    StringData data=new StringData();
		int i=drugInventoryCheckService.delete(drugInventoryCheck);
        if(i>0){
            data.setCode("success");
            data.setData("删除药品盘点成功");
        }else{
            data.setCode("error");
            data.setData("删除药品盘点失败");
        }
		return data;
	}

	/**
	 * 查询药品盘点记录
	 * @param drugInventoryCheck
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author 	CTQ
	 * @date 2017-07-05 18:45:46
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"recordIndex", ""})
	public String recordIndex(DrugInventoryCheck drugInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugStock> page = drugStockService.findRecordPage(new Page<DrugStock>(request, response), drugInventoryCheck);
		model.addAttribute("page", page);
		return "modules/drug/drugInventoryCheckRecordIndex";
	}

	/**
	 * 查询药品盘点记录明细
	 * @param drugInventoryCheck
	 * @param model
	 * @return
	 * @author CTQ
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "recordDetail")
	public String recordDetail(DrugInventoryCheck drugInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugInventoryCheck> page = drugInventoryCheckService.findPage(new Page<DrugInventoryCheck>(request, response), drugInventoryCheck);
		model.addAttribute("page", page);
		return "modules/drug/drugInventoryCheckRecordDetail";
	}

	/**
	 * 药品盘点盈亏统计
	 * @param drugInventoryCheck
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"profitLossIndex", ""})
	public String profitLossIndex(DrugInventoryCheck drugInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugInventoryCheck> page = drugInventoryCheckService.findProfigLossPage(new Page<DrugInventoryCheck>(request, response), drugInventoryCheck);
		model.addAttribute("page", page);
		return "modules/drug/drugInventoryCheckProfitLossIndex";
	}

}